package me.maiz.tool.codegen.controller;


import java.util.Arrays;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import me.maiz.tool.codegen.common.Result;
import me.maiz.tool.codegen.domain.AdminDO;
import me.maiz.tool.codegen.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-09-28 16:40:34 | Aron</small>
 */
@Controller
@RequestMapping("/ifast/admin")
public class AdminController extends AdminBaseController {

	@Autowired
	private AdminService adminService;
	
	@GetMapping()
	String Admin(){
	    return "ifast/admin/admin";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<AdminDO>> list(AdminDO adminDTO){
        Wrapper<AdminDO> wrapper = new EntityWrapper<AdminDO>().orderBy("admin_id", false);
        Page<AdminDO> page = adminService.selectPage(getPage(AdminDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	String add(){
	    return "ifast/admin/add";
	}

	@GetMapping("/edit/{adminId}")
	String edit(@PathVariable("adminId") Integer adminId,Model model){
		AdminDO admin = adminService.selectById(adminId);
		model.addAttribute("admin", admin);
	    return "ifast/admin/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( AdminDO admin){
		adminService.insert(admin);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( AdminDO admin){
		boolean update = adminService.updateById(admin);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Integer adminId){
		adminService.deleteById(adminId);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Integer[] adminIds){
		adminService.deleteBatchIds(Arrays.asList(adminIds));
		return Result.ok();
	}
	
}
